/**
	*****************************************************************************
	* @file     cmem7_usart.c
	*
	* @brief    CMEM7 uart file
	*
	*
	* @version  V1.0
	* @date     3. September 2013
	*
	* @note               
	*           
	*****************************************************************************
	* @attention
	*
	* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
	* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
	* TIME. AS A RESULT, CAPITAL-MICRO SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 
	* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
	* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
	* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
	*
	* <h2><center>&copy; COPYRIGHT 2013 Capital-micro </center></h2>
	*****************************************************************************
	*/

#include "cmem7_usart.h"

#define USART_Mode_8b                        1
#define USART_Mode_8b_Parity                 7

#define USART_BaudMode_0                     0
#define USART_BaudMode_1                     1
#define USART_BaudMode_Division							 19200

#define USART_WR_MAX_FIFO_SIZE               16	

static uint16_t usart_CalcBaudrateReload(uint32_t FreqHz, uint32_t Baudrate) 
{
  if (Baudrate <= USART_BaudMode_Division) {                     \
		/** reload in mode 0
		  *    reload = FreqHz / 16 * Baudrate
		  * round up 
		  *    reload = FreqHz / 16 * Baudrate + 1/ 2
		  *    reload = (2 * FreqHz + 16 * Baudrate) / 2 * 16 * Baudrate
		  */
		return ((FreqHz << 1) + (Baudrate << 4)) / (Baudrate << 5);
	} 
	
	/** reload in mode 1
		*    reload = Baudrate * 16 * 65536 / FreqHz
		* round up 
		*    reload = Baudrate * 16 * 65536 / FreqHz + 1/ 2
		*    reload = (2 * Baudrate * 16 * 65536 + FreqHz) / 2 * FreqHz
		*/
	return ((((uint64_t)(Baudrate)) << 21) + FreqHz) / (FreqHz << 1);
}
            
void USART_Init(USART0_Type* USARTx, USART_InitTypeDef *init) {
 	assert_param(IS_USART_ALL_PERIPH(USARTx));
	assert_param(init);
 	assert_param(IS_USART_STOPBITS(init->USART_StopBits));
 	assert_param(IS_USART_PARITY(init->USART_Parity));
	
	/* TODO : assume clock is 50MHz */
	USARTx->BAUDRATE = usart_CalcBaudrateReload(
	  SYSTEM_CLOCK_FREQ, init->USART_BaudRate);
	USARTx->CTRL_b.MODE = 
	  (init->USART_Parity == USART_Parity_None) ? 
	  USART_Mode_8b : USART_Mode_8b_Parity;	
	USARTx->CTRL_b.STOP = init->USART_StopBits;
	USARTx->CTRL_b.PARITY = 
	  (init->USART_Parity == USART_Parity_None) ? 
	  USART_Parity_Even : init->USART_Parity;
	USARTx->CTRL_b.LOOPBACK = init->USART_LoopBack;
	USARTx->CTRL_b.RX_EN = init->USART_RxEn;
	USARTx->CTRL_b.CTS = init->USART_CtsEn;
	USARTx->CTRL_b.BAUD_MODE = 
	  (init->USART_BaudRate > USART_BaudMode_Division) ? 
	  USART_BaudMode_1 : USART_BaudMode_0;
	USARTx->CTRL_b.FIFO_EN = TRUE;
	USARTx->CTRL_b.RX_THRESHOLD = USART_WR_MAX_FIFO_SIZE;
	USARTx->CTRL_b.RX_HALF_FULL = (USART_WR_MAX_FIFO_SIZE >> 1);
	USARTx->TIMEOUT = 0xFF;
	
	USARTx->INT_MASK |= USART_Int_All;
	USARTx->INT_SEEN &= USART_Int_All;
}

void USART_EnableInt(USART0_Type* USARTx, uint32_t Int, BOOL enable) {
	assert_param(IS_USART_ALL_PERIPH(USARTx));
	assert_param(IS_USART_INT(Int));
	
  if (enable) {
	  USARTx->INT_MASK &= ~Int;
	} else {
		USARTx->INT_MASK |= Int;
	}
	
	USARTx->INT_MASK &= USART_Int_All;
}

void USART_Enable(USART0_Type* USARTx, BOOL enable) {
	assert_param(IS_USART_ALL_PERIPH(USARTx));
	
	USARTx->RUN_b.EN = enable;
}

BOOL USART_GetIntStatus(USART0_Type* USARTx, uint32_t Int) {
	assert_param(IS_USART_ALL_PERIPH(USARTx));
	assert_param(IS_USART_INT(Int));
	
	if (0 != (USARTx->INT_SEEN & Int)) {
		return TRUE;
	}
	
	return FALSE;
}

void USART_ClearInt(USART0_Type* USARTx, uint32_t Int) {
	assert_param(IS_USART_ALL_PERIPH(USARTx));
	assert_param(IS_USART_INT(Int));
	
	USARTx->INT_SEEN |= Int;
}

uint8_t USART_Write(USART0_Type* USARTx, uint8_t Size, uint8_t* Data) {
	uint8_t count;
	
	/* Check the parameters */
  assert_param(IS_USART_ALL_PERIPH(USARTx));
	assert_param(Data);
	
	if (!USARTx->RUN_b.EN) {
		return 0;
	}
	
	count = 0;
	while (!USARTx->STATUS_b.TF && count < Size) {
		USARTx->TX_BUF = *(Data + count++);
	}
  
	return count;
}

/* return value is actual read data size */
uint8_t USART_Read(USART0_Type* USARTx, uint8_t Size, uint8_t* Data) {
	uint8_t count;
	
	assert_param(IS_USART_ALL_PERIPH(USARTx));
	assert_param(Data);
	
	if (!USARTx->RUN_b.EN) {
		return 0;
	}
	
	count = 0;
	while (USARTx->STATUS_b.RNE && count < Size) {
		*(Data + count++) = (USARTx->RX_BUF & 0x00FF);
	}
	
	return count;	
}

